﻿using LiteDB;
using SmartCMS.UI.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace SmartCMS.UI.Helper
{
    public static class DBHelper
    {
        private static string SmartCMSDB = @"SmartCMS.db";
        private static LiteDatabase GetDatabase()
        {
            using (var db = new LiteDatabase(SmartCMSDB))
            {
                return db;
            }
        }
        public static LiteCollection<T> GetCollection<T>() where T : class
        {
            return GetDatabase().GetCollection<T>();
        }
        public static bool InsertExecute<T>(T model) where T : class
        {
            try
            {
                GetCollection<T>().Insert(model);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        public static bool UpdateExecute<T>(T model) where T : class
        {
            try
            {
                GetCollection<T>().Update(model);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        public static void Demo()
        {
            for (int i = 1; i < 100; i++)
            {
                UserInfo model = new UserInfo();
                model.Id = i;
                model.UserName = "张善友" + i;
                model.UserPwd = "张善友" + i;
                model.Sex = "变态";
                InsertExecute(model);
            }
        }
        public static void GetList()
        {

        }
    }
}
